package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.RbacPerm;
import com.woniuxy.servcie.RbacPermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 佘赛飞
 * @create 2021-11-26 8:55 下午
 */
@RestController
@RequestMapping("/perm")
public class RbacPermController {
    @Autowired
    RbacPermService rbacPermService;
    @GetMapping("/findOneRbacPerm/{id}")
    public RbacPerm findOneRbacPerm(@PathVariable int id){
        return rbacPermService.findOneRbacPerm(id);
    }

    /**
     * 通过角色ID数组，来查询中间表，然后再通过中间表查出所有权限
     * @param roles
     * @return
     */
    @RequestMapping("findAllPerm")
    public List <RbacPerm> findAll(@RequestBody int[] roles){
        return rbacPermService.findAllRbacPerms(roles);
    }

    @RequestMapping("/addRbacPerm")
    public void addRbacPerm(@RequestBody RbacPerm rbacPerm){
       rbacPermService.addRbacPerm(rbacPerm);
    }

    @RequestMapping("/updateRbacPerm")
    public Integer UpdateRbacPerm(@RequestBody RbacPerm rbacPerm){
        return rbacPermService.UpdateRbacPerm(rbacPerm);
    }
    @RequestMapping("/deleteRbacPerm")
    public void deleteRbacPerm(@RequestBody RbacPerm rbacPerm){
        rbacPermService.deleteRbacPerm(rbacPerm);
    }
    @GetMapping("/FindAllAndPaginationRbacPerm/{startPage}/{pageSize}")
    public PageInfo<RbacPerm>FindAllAndPaginationRbacPerm(@PathVariable("startPage") int startPage
            ,@PathVariable("pageSize") int pageSize){
        PageHelper.startPage(startPage,pageSize);
        List <RbacPerm> rbacPerms = rbacPermService.FindAllAndPaginationRbacPerm();
        PageInfo<RbacPerm> permPageInfo = new  PageInfo<>(rbacPerms);
        return permPageInfo;
    }

    @GetMapping("/findAllResRbacPerm/{startPage}/{pageSize}")
    public ResponseEntity<PageInfo<RbacPerm>>findAllResRbacPerm(@PathVariable("startPage") int startPage
            ,@PathVariable("pageSize") int pageSize){
        PageHelper.startPage(startPage,pageSize);
        List <RbacPerm> rbacPerms = rbacPermService.FindAllAndPaginationRbacPerm();
        PageInfo<RbacPerm> permPageInfo = new  PageInfo<>(rbacPerms);
        return new ResponseEntity(200,"查询成功",permPageInfo);
    }
}
